package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author YinBR
 * @email sunlightcs@gmail.com
 * @date 2023-07-28 11:08:41
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;



//    /product/attrgroup/{catelogId}/withattr
    @GetMapping("{catelogId}/withattr")
    public R AttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVo = attrService.getCatelogIdWithattr(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVo);
    }
    public static void main(String[] args) {
        // 待编码的字符串
        String encodedString = "M190QFVfcmV0MXJXX3Njy24=";

        // 进行 Base64 编码
//        String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
//        System.out.println("Encoded string: " + encodedString);

        // 进行 Base64 解码
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);
        System.out.println("Decoded string: " + decodedString);
    }

    /**
     * 新增属性分组关联中的新建关联
     * @param vos
     * @return
     */
//    /product/attrgroup/attr/relation
    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        attrAttrgroupRelationService.addRelation(vos);
        return R.ok();

    }



    /**
     * 删除属性分组关联数据
     * @param attrGroupRelationVos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrService.deleteRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 查询所有新建关联中未被关联的数据
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 根据pms_attr_attrgroup_relation表中的属性分组id查询商品属性列表
     * 获取属性分组的关联的所有属性
     * @param attrgroupId 属性分组id
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities =  attrService.getRelationAttr(attrgroupId);
        if (entities == null){
            return R.error(BizCodeEnum.RETURN_NULL.getCode(), BizCodeEnum.RETURN_NULL.getMsg());

        }        return R.ok().put("data",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params,catelogId);
        PageUtils page = attrGroupService.queryCateLogIdPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 分类详细信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //查询分类信息层级结构 例如家电/电视/海信
        Long catelogId = attrGroup.getCatelogId();
        Long[] brandIdList = categoryService.findByAttrId(catelogId);
        attrGroup.setCatelogPath(brandIdList);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


}
